import _ from 'lodash';

export const urlValid = function (value) {
    const urlReg = /(http|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/;

    if (!!value) {
        if (urlReg.test(value)) {
            return true;
        } else {
            return '地址格式不正确';
        }
    } else {
        return true;
    }
}

export const textLengthValid = function (length = 0) {
    return function (value) {
        if (!!value) {
            if (_.toString(value).length < length) {
                return true;
            } else {
                return `不得超过${length}个字符`;
            }
        } else {
            return true;
        }
    }
}

export const timeValid = function (time) {

    if (!!time && time.length > 0) {
        let beginDate = new Date(time[0]).getTime();
        let endDate = new Date(time[1]).getTime();

        if (endDate < Date.now()) {
            return "结束时间必须大于当前时间!";
        } else if (beginDate === endDate) {
            return "结束时间必须大于开始时间!";
        }
    } else {
        return true;
    }
}

export const mobileValid = function (mobile) {
    var reg = /^(((13[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(18[0-9]{1}))+\d{8})$/;

    if (!reg.test(mobile)) {
        return '手机号码无效!';
    } else {
        return true;
    }
}

export const repPsdValid = function (store, name) {
    return function (value) {
        if (_.toString(store.fields[name].value) === _.toString(value)) {
            return true;
        } else {
            return '密码不一致!';
        }
    }
}

export const isEnglishAndNumber = (function () {
    const reg = /^[0-9a-zA-Z]+$/;
    const text = '只能为英文和数字';
    return function (value) {
        if (reg.test(value)) {
            return true
        } else {
            return text;
        }
    }
})();

export const isNotHan = (function () {
    const reg = /[\x00-\xff]+$/;
    const text = '不能为汉字';
    return function (value) {
        if (reg.test(value)) {
            return true
        } else {
            return text;
        }
    }
})();